package com.lagou.user.controller;

import com.lagou.code.service.CodeService;
import com.lagou.user.bean.User;
import com.lagou.user.dao.UserRepository;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("register")
public class RegisterController {

    @Reference
    CodeService codeService;

    @Autowired
    UserRepository userRepository;

    @GetMapping("/{email}/{password}/{code}")
    public int register(@PathVariable("email") String email,
                         @PathVariable("password") String password,
                         @PathVariable("code") String code){
        if(codeService.validateCode(email, code) > 0){
            return 2;
        }
        User user = userRepository.findByEmail(email);
        if(user != null){
            return 1;
        }
        user = new User();
        user.setEmail(email);
        user.setPassword(password);
        userRepository.save(user);
        return 0;
    }
}
